<?php

/***************************************************************\
* FILE:		sys/pages/dir.inc
* SECTION:	pages > core set
* FUNCTION:	give an overview of the contents of a directory
* USES:	
* INCLUDES:		
* PRODUCES:	
* LAUNCHES:	
\***************************************************************/

include_once('sys/forumfunctions.inc');


/**
 * adds extra access checks, dependent on page state
 */
function page_inc_check_access() {
	check_access("READ");
}

/**
 *
 */
function page_inc_execute_action($action) {
	global $PAGE;
	$THISDIR = $PAGE['directory_tree'][$PAGE['dir_depth']-1];

	switch($action) {
		case "markread-sub":
			if (!$_SESSION['user_id'])
				errorpage(6);
			if ($PAGE['id'] != 0) { // alle fora
				$from = array("ITEM_IN INNER JOIN SUB_IN ON ITEM_IN.sub_id = SUB_IN.sub_id", "DUMMY");
				$where = "SUB_IN.dir_id IN (" . implode(",", $PAGE['menu'][$PAGE['id']]['children']) . ")";
				$sql = db_query_select(array(array(), "ITEM_IN.item_id"), $from, $where);
			} else
				$sql = db_query_select(array(array("item_id")), array("ITEM_IN"), 1);
			$sqlresult = db_do_query($sql);
			$values = array();
			while ($r = db_fetch_array($sqlresult))
				array_push($values, array($r['item_id'], $_SESSION['user_id']));
			if (count($values)) {
				$sql = db_query_insert_multiple(array(item_id, user_id), $values, "HAS_READ", 1);
				db_do_query($sql);
			}
			reset_menu_new($THISDIR['dir_id']);
			$menu = $PAGE['menu'][$PAGE['id']];
			$i = $menu['ignore'] ? 1 : 0;
			reset($menu['children']);
			do {
				$kid = current($menu['children']);
				$i += $PAGE['menu'][$kid]['ignore'];
			} while (next($menu['children']));
			if ($i < 1)
				add_alert("Als bepaalde directories je niet interesseren, kan je ze negeren in het actiemenu in die directory. " . 
					  "In het menu verschijnt dan niet meer telkens de melding dat er niets nieuws is gebeurd en zo bespaar " . 
					  "je heel wat pagina-berekeningstijd");
			add_alert("alles gelezen hier...");
			break;
		default:
			add_alert("unknown action: $action");
			break;
	}
}

function reset_menu_new($dir) {
	global $PAGE;
	$subs = $PAGE['menu'][$dir]['children'];
	reset($subs);
	do {
		$sub = current($subs);
		$PAGE['menu'][$sub]['new_items'] = 0;
		if (is_array($PAGE['menu'][$sub]['children']) and count($PAGE['menu'][$sub]['children']))
			reset_menu_new($sub);
	} while (next($subs));
}

/**
 * execute page logic
 */
function page_inc_execute() {
	
	global $PAGE;
	
	$PAGE['content']['title'] = "Subfora";

	if ($_SESSION['user_id'])
		// mark all items read
		add_action('markread-sub', 'url-action', 'markread-sub');

	if ($PAGE['id'] == 0)
		$PAGE['subfora'] = fora_fetch_multiple($PAGE['id'], $PAGE['menu'], array());
	else
		$PAGE['subfora'] = array(array('fora' => fora_get_subfora($PAGE['id'], $PAGE['menu']), 'title' => $PAGE['menu'][$PAGE['id']]['title']));
}


/**
 * dress up page content using skin stuff
 */
function page_inc_dress() {
	global $PAGE;

	fora_page_inc_check_occurences();
	if($PAGE['occurences']) {
		print_occurences($PAGE['occurences']);
	}

	if ($PAGE['subfora'])
		print_subfora($PAGE['subfora'], $PAGE['dirpath'], 1);
	
	if($PAGE['occurences']) {
		print_occurences($PAGE['occurences']);
	}
}

?>
